package com.example.demo.scheduledtask;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.demo.common.Constant;
import com.example.demo.config.ErpConfig;
import com.example.demo.mapper.PoMpsinfoMapper;
import com.example.demo.mapper.WmsPurchaseMapper;
import com.example.demo.utils.SoapXmlUtil;
import com.example.demo.vo.PurchaseReceiptFormVoReq;
import com.example.demo.vo.TransferInVo;
import com.example.demo.vo.XmlResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author : Zhang.yt
 * @date : 2024/7/29
 */
@Component
@Slf4j
@DS("wms")
public class TransferInCallback {
    @Resource
    private WmsPurchaseMapper wmsPurchaseMapper;

    @Autowired
    private SoapXmlUtil soapXmlUtil;

    @Autowired
    PoMpsinfoMapper poMpsinfoMapper;

    @Resource
    private ErpConfig erpConfig;

    /**
     * 二阶调拨拨入过账单回调
     */
    @Scheduled(fixedDelay = 23 * 1000)//开启定时同步
    //@Async(value = "taskExecutor")
    public void transferInCallback() {
        //查询没有回调的数据
        TransferInVo transferInVo = wmsPurchaseMapper.getTransferInCallback();
        if (ObjectUtil.isEmpty(transferInVo)) {
            log.info("未查询到二阶段调拨拨入过账单数据，修改过账失败数据重新进行回写");
            wmsPurchaseMapper.transferInCallbackLose();
            return;
        }
        transferInVo.setActionType(Constant.ActionType_I);
        transferInVo.setIndcsite(Constant.PMDSSITE_AMA);
        Class<PurchaseReceiptFormVoReq> target = PurchaseReceiptFormVoReq.class;
        BeanUtils.copyProperties(transferInVo, target);

        String resMessage = soapXmlUtil.getSoapXml("TransferIn", "indc_t", transferInVo);
        try {

            //转换为soap消息
            log.info("二阶段调拨拨入过账单回调：{}", resMessage);
            wmsPurchaseMapper.updateWarehouseTransferInCallback(transferInVo.getIndcdocno(), "erp待返回", 3);
            //发送soap消息，并解析结果
            String result;
            try {
                result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.getUrl());
            } catch (Exception e) {
                result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.URL2);
            }
            log.info("二阶段调拨拨入过账单回调返回结果: {}", result);
            XmlResp xmlResp = soapXmlUtil.analysisXml(result);
            if (xmlResp.getCode().equals("0")) {
                //更新相应表状态为已下发
                wmsPurchaseMapper.updateWarehouseTransferInCallback(transferInVo.getIndcdocno(), xmlResp.getData().get("indcdocno"), 1);
            } else {
                wmsPurchaseMapper.updateWarehouseTransferInCallback(transferInVo.getIndcdocno(), xmlResp.getMessage(), 2);
            }
        } catch (Exception e) {
            wmsPurchaseMapper.updateWarehouseTransferInCallback(transferInVo.getIndcdocno(), "非常见的数据处理问题，请联系开发人员处理", 2);
            e.printStackTrace();
        }

    }
}
